package top.drinyeit.nongmao.fragment;

import android.databinding.DataBindingUtil;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.scu.miomin.shswiperefresh.core.SHSwipeRefreshLayout;

import top.drinyeit.nongmao.R;
import top.drinyeit.nongmao.databinding.CartView;
import top.drinyeit.nongmao.viewmodel.CartVM;

/**
 * Created by YJY on 2018/1/10.
 */

public class CartFragment extends LazyFragment2 {

    CartVM cartVM;
    CartView mCartView;
    public RecyclerView cartList;
    public SHSwipeRefreshLayout shSwipeRefreshLayout;

    int statusBarHeight1 = -1;//状态栏高度

    @Override
    protected View initDatabindingAndView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mCartView = DataBindingUtil.inflate(inflater, R.layout.fragment_cart, container, false);
        initList();
        return mCartView.getRoot();
    }

    @Override
    protected void lazyLoad() {
        cartVM =  new CartVM(this);
        mCartView.setICartView(cartVM);
    }

    private void initList() {
        shSwipeRefreshLayout = (SHSwipeRefreshLayout) mCartView.getRoot().findViewById(R.id.srl_cart);
        cartList = (RecyclerView) mCartView.getRoot().findViewById(R.id.list_cart);
        cartList.setLayoutManager(new LinearLayoutManager(getActivity()));
        cartList.setHasFixedSize(true);

        setTitle(false);
    }
    

    @Override
    public void setUserVisibleHint(boolean isVisibleToUser) {
        super.setUserVisibleHint(isVisibleToUser);
        if (isVisibleToUser) {
            //相当于Fragment的onResume
            cartVM.getCartList();
        } else {
            //相当于Fragment的onPause
        }
    }

//    @Override
//    public void onHiddenChanged(boolean hidden) {
//        super.onHiddenChanged(hidden);
//        if (hidden) {
//            //相当于Fragment的onResume
//            mCartView.setICartView(new CartVM(this));
//        } else {
//            //相当于Fragment的onPause
//            mCartView.setICartView(new CartVM(this));
//        }
//    }

    public void setTitle(boolean isBack){
        int resourceId = getResources().getIdentifier("status_bar_height", "dimen", "android");
        if (resourceId > 0) {
            //根据资源ID获取响应的尺寸值
            statusBarHeight1 = getResources().getDimensionPixelOffset(resourceId);
        }
        Log.e("WangJ", "状态栏-高度:" + statusBarHeight1);

        View state = mCartView.getRoot().findViewById(R.id.v_state);

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP){
            state.setBackgroundColor(ContextCompat.getColor(this.getActivity(),R.color.transparent_03));
        }else {
            state.setBackgroundColor(ContextCompat.getColor(this.getActivity(),R.color.transparent));
        }

    }

}